﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Turtle8
{
    public partial class SoldierCard : UserControl
    {
        public enum PanelState { Default = 1, OpenSoldierDataCard, NewSoldierDataCard};

        DataPanel dataPanel; 
        SearchCard searchCard;
        PictureBox picBoxMain = new System.Windows.Forms.PictureBox();
        SoldierQualifiedViewCard qualifiedViewCard;

        public SoldierCard()
        {
            InitializeComponent();
            dataPanel = new DataPanel(this);
            searchCard = new SearchCard(this);
            qualifiedViewCard = new SoldierQualifiedViewCard();

            picBoxMain.SizeMode = PictureBoxSizeMode.StretchImage;
            String currentDir = System.IO.Directory.GetCurrentDirectory();
            picBoxMain.Image = Image.FromFile(currentDir + @"\images\Soldier.jpg");
            picBoxMain.Dock = DockStyle.Fill;
            picBoxMain.BorderStyle = BorderStyle.FixedSingle;

            this.Dock = DockStyle.Fill;
        }

        public void changeMainPanel(Control ctrl)
        {
            if (!panelMain.Controls.Contains(ctrl))
            {
                panelMain.Controls.Clear();
                panelMain.Controls.Add(ctrl);
            }
        }
        public void changeMainPanel(PanelState state, string army_id)
        {
            panelMain.Controls.Clear();
            switch (state)
            {
                case PanelState.Default:         
                    panelMain.Controls.Add(picBoxMain); 
                    break;

                case PanelState.OpenSoldierDataCard:
                    dataPanel = new DataPanel(this);
                    panelMain.Controls.Add(dataPanel);
                    String selectStr = CompanyManager.getSqlStringFromFile(@"\SELECT\SoldierCardPanel.SELECT.sql");

                    SqlCommand selectCmd = SqlManager.SqlCommandFactory(selectStr);
                    selectCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = army_id;
                    SqlDataReader reader = selectCmd.ExecuteReader();
                    dataPanel.Fill(reader);
                    dataPanel.maskedTextBoxArmyId.Enabled = false;
                    reader.Close();                    
                    break;

                case PanelState.NewSoldierDataCard:

                    dataPanel = new DataPanel(this);
                    panelMain.Controls.Add(dataPanel);
                    break;

                default: break;
            }
        }

        /// <summary>
        /// main event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SoldierDataCard_Load(object sender, EventArgs e)
        {
            changeMainPanel(picBoxMain);
        }

        //event handlers
        private void buttonNewCard_Click(object sender, EventArgs e)
        {
            dataPanel = new DataPanel(this);
            changeMainPanel(dataPanel);
            textBoxTopBar.Text = "כרטיס לוחם - פרטים מלאים";
        }
        private void buttonSearchCard_Click(object sender, EventArgs e)
        {
            changeMainPanel(searchCard);
            textBoxTopBar.Text = "כרטיס לוחם - חיפוש";
        }

        private void buttonMainPage_Click(object sender, EventArgs e)
        {
            changeMainPanel(picBoxMain);
            textBoxTopBar.Text = "כרטיס לוחם - מסך ראשי";
        }

        private void buttonQualifiedView_Click(object sender, EventArgs e)
        {
            changeMainPanel(qualifiedViewCard);
            textBoxTopBar.Text = "כרטיס לוחם - מעקב כשירויות";
        }

        public void refresh()
        {
            qualifiedViewCard.refresh();
            searchCard.refresh();
        }
    }
}
